package com.gxa.jjty.admin.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.service.AppServeService;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.AppServe;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author asus
 */
@RestController
@RequestMapping("/appServe")
@Api(tags = "应用服务管理")
@Log4j2
public class AppController {

    @Autowired
    AppServeService appServeService;

    @PostMapping("/getById")
    @ApiOperation(value = "查看")
    public Map selectById(@RequestParam("id") Long id ) {
        AppServe appServe = appServeService.selectById(id);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(appServe);
        return jsonResult.getDataMap();
    }

    @PostMapping("/get")
    @ApiOperation(value ="筛选")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码"),
            @ApiImplicitParam(name = "size",value = "每页记录数"),
            @ApiImplicitParam(name = "appName",value = "服务名称"),
            @ApiImplicitParam(name = "appStatus",value = "应用状态"),
    })
    public  Map selectByPage( Integer page,
                        Integer size,
                        String appName,
                        Integer appStatus){
        PageInfo<AppServe> appServePageInfo = appServeService.selectByPage(page, size, appName, appStatus);
        List<AppServe> list = appServePageInfo.getList();
        long total = appServePageInfo.getTotal();

        JsonResult jsonResult = new JsonResult();
        jsonResult.put("total",total);
        jsonResult.setCode("1");
        jsonResult.setData(list);
        return jsonResult.getDataMap();
    }

    @PostMapping("/putApp")
    @ApiOperation("添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appName",value = "服务名称"),
            @ApiImplicitParam(name = "appIcon",value = "应用图标"),
            @ApiImplicitParam(name = "h5",value = "应用服务域名"),
            @ApiImplicitParam(name = "appDescribe",value = "应用服务描述"),
            @ApiImplicitParam(name = "appStatus",value = "应用服务状态"),
            @ApiImplicitParam(name = "cost",value = "费用"),
            @ApiImplicitParam(name = "content",value = "服务内容")
    })
    public Map insert(@RequestBody AppServe appServe) throws BusinessException {
        appServeService.insertApp(appServe);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/set")
    @ApiOperation("编辑")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appName",value = "服务名称"),
            @ApiImplicitParam(name = "appIcon",value = "应用图标"),
            @ApiImplicitParam(name = "h5",value = "应用服务域名"),
            @ApiImplicitParam(name = "appDescribe",value = "应用服务描述"),
            @ApiImplicitParam(name = "appStatus",value = "应用服务状态"),
            @ApiImplicitParam(name = "cost",value = "费用"),
            @ApiImplicitParam(name = "content",value = "服务内容"),
            @ApiImplicitParam(name = "id",value = "id")
    })
    public Map update(@RequestBody AppServe appServe) throws BusinessException {
        System.out.println(appServe.getAppIcon());
        appServeService.updateAppServe(appServe);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("更新成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/delete")
    @ApiOperation("删除")
    public  Map delete(@RequestParam ("id") Long id) throws BusinessException {
        appServeService.deleteAppServe(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }



}
